//
//  DWPersonViewController.m
//  OCAPPDemo
//
//  Created by Apple on 2021/3/24.
//

#import "DWPersonViewController.h"
#import <objc/runtime.h>
#import "DWPerson.h"
#import "DWPerson+associateObject.h"


@interface DWPersonViewController () {
    NSString *aaa;
    char c;
}

@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy, readonly) NSString *text;
@property (nonatomic, assign, getter=isEnd, setter=setA:) BOOL end;
@property (nonatomic, strong) DWPerson *p1;
@property (nonatomic, assign) int num;

@end

@implementation DWPersonViewController
//@synthesize p1 = _p1;
//@dynamic age;

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.p1 = [DWPerson new];
    
    self.age = 10;
    
    self.p1.age = 10;
    
    self.p1.name = @"Person";
    
    //[self printIvarsWithObj:self.p1];
    
}

- (IBAction)setIVars:(id)sender {
    
    self.p1.name = @"newName";
    self.p1.age = 20;
    self.p1.weight = 90;
    
}


- (IBAction)printIVars:(id)sender {
    
    NSLog(@"name = %@", self.p1.name);
    NSLog(@"age = %lu", (unsigned long)self.p1.age);
    NSLog(@"weight = %d", self.p1.weight);
}



- (void)printIvarsWithObj:(id)obj {
    unsigned int count;
    Ivar *ivars = class_copyIvarList([obj class], &count);
    for (int i = 0; i < count; ++i) {
        Ivar ivar = ivars[i];
        const char *memberName = ivar_getName(ivar);
        NSLog(@"变量名 = %s",memberName);
    }
}


- (void)dealloc {
    NSLog(@"%s", __FUNCTION__);
}

- (void)setText:(NSString *)text {
    _text = [text copy];
}

//- (void)setP1:(DWPerson *)p1 {
//
//}
//
//- (DWPerson *)p1 {
//    return [DWPerson new];
//}

- (void)testGetterAndGetter {
    if (![self isEnd]) {
        [self setA:YES];
    }
    NSLog(@"%d", self.end);
}

- (void)testReadonly {
    // readonly 测试代码
    NSLog(@"%@", self.text);
    self.text = @"a";
    NSLog(@"%@", self.text);
}


- (IBAction)testCopy:(id)sender {
    //copy 测试代码
    //NSMutableArray *a = [[NSMutableArray alloc] initWithArray:@[@"jack", @"rose"]];
    NSMutableString *mstr = [NSMutableString stringWithFormat:@"123"];
    NSString *str = @"123";
    self.name = mstr; // 深拷贝
    NSLog(@"%p, %p", str, _name);
    NSLog(@"first-%@", _name);
    str = @"456";
    NSLog(@"second-%@", _name);
    NSLog(@"%p, %p", str, _name);
    //NSLog(@"%@ %@", object_getClass(self.names), object_getClass(a));
    //[self.names appendString:@"Jone"];
    //NSMutableArray *b = self.names;
    //[self.names addObject:@"Dave"];
    //NSLog(@"%p, %p, %p", a, _names, b);

}


@end
